<?php
/**
 * Created by PhpStorm.
 * User: raoho
 * Date: 2018/1/3
 * Time: 17:26
 */

namespace Xcx\Model;

class CartModel extends BaseModel
{

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 将商品添加至购物车
     * @param $gid
     * @param $sepcID
     * @param $count
     * @return mixed
     */
    public function addCart($gid,$sepcID,$count)
    {
        $data['uid'] = $this->uid;
        $data['gid'] = $gid;
        $data['spec_id'] = $sepcID;
        $data['count'] = $count;
        $data['add_time'] = time();

        return $this->data($data)->add();
    }

    /**
     * 检查商品是否在购物车中
     * @param $gid
     * @param $specID
     * @return mixed
     */
    public function checkGoodsIsInCart($gid,$specID)
    {
        $maps['uid'] = $this->uid;
        $maps['gid'] = $gid;
        $maps['spec_id'] = $specID;
        return $this->where($maps)->find();
    }

    /**
     * 更新购物车中的商品
     * @param $gid
     * @param $specID
     * @param $count
     * @return bool
     */
    public function updateCart($gid,$specID,$count)
    {
        $maps['uid'] = $this->uid;
        $maps['gid'] = $gid;
        $maps['spec_id'] = $specID;

        return $this->where($maps)->data(array('count'=>$count))->save();
    }

    /**
     * 获取购物车列表
     * @return mixed
     */
    public function getCartList()
    {
        $field = 'a.*,b.img,b.name,b.intro2,c.name spec_name,c.price';
        $list = $this->alias('a')
            ->field($field)
            ->join('left join t_goods b on a.gid = b.id')
            ->join('left join t_goods_spec c on a.spec_id = c.id')
            ->where(array('uid'=>$this->uid))
            ->select();
        if (is_array($list) && count($list) > 0) {
            $list = getUploadImgDir($list,array('goods'=>'img'),2);
            return $list;
        }
        return array();
    }

    /**
     * 删除商品
     * @param $id
     * @return mixed
     */
    public function removeCart($id)
    {
        $maps['uid'] = $this->uid;
        $maps['id'] = $id;
        return $this->where($maps)->delete();
    }

    /**
     * 确认订单，删除购物车
     * @param $id
     * @param $spec
     * @return mixed
     */
    public function deleteCartList($id,$spec)
    {

        $maps['uid'] = $this->uid;
        $maps['gid'] = array('IN',$id);
        $maps['spec_id'] = array('IN',$spec);
        return $this->where($maps)->delete();
    }

    /**
     * 增加商品数量
     * @param $id
     * @return bool
     */
    public function setIncCount($id)
    {
        return $this->where(array('id'=>$id,'uid'=>$this->uid))->setInc('count');
    }

    /**
     * 减少商品数量
     * @param $id
     * @return bool
     */
    public function setDecCount($id)
    {
        return $this->where(array('id'=>$id,'uid'=>$this->uid))->setDec('count');
    }

    /**
     * 获取确认订单购物车列表
     * @param $idList array 购物车ID数组
     * @return mixed
     */
    public function getConfirmGoodsList($idList)
    {
        $maps['a.id'] = array('IN',$idList);
        return $this->alias('a')
            ->field('a.*,b.price')
            ->join('left join t_goods_spec b on a.spec_id = b.id')
            ->where($maps)
            ->select();
    }
}